
* Table 4 and 5
qui{
* 1. first calculate effects of sick days on earnings - will need this in the second step
qui{
clear 
use "Experiments Stata Files/Experiment0.dta"
gen lost_earnings= (40-hours)* wage_offer*52 if offer_accepted==4 | offer_accepted==5
replace lost_earnings= (20-hours)* wage_offer*52 if (offer_accepted==2 | offer_accepted==3) 


gen earnings_total = earnings + lost_earnings
replace earnings_total =0 if earnings_total==.
keep if age<65
replace sick_days=0 if offer_accepted==1 | offer==1 | offer==.

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period) 
 
gen discounted_earnings_total=earnings_total*discounting_factor
bysort experiment ID: egen earnings_PV_total = total(discounted_earnings_total)
keep if age==25

rename earnings_PV_total earnings_PV

preserve
inequal7 earnings_PV if experiment==0
gen COV=r(cov) if experiment==0
gen GINI=r(gini) if experiment==0
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI , by(experiment) 
gen education=0
sort education experiment
save "Temp\PV Earnings Variation SD.dta", replace
restore 

* by education

preserve
gen COV=.
gen GINI=.
tostring COV  GINI, replace
foreach ed of numlist 1 2 3 {
inequal7 earnings_PV if experiment==0 &  education==`ed'
replace COV=r(cov) if experiment==0 & education==`ed'
replace GINI=r(gini) if experiment==0 & education==`ed'
}
destring COV GINI , replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education) 
sort education experiment
append using "Temp\PV Earnings Variation SD.dta"
reshape wide MEAN COV  GINI , i(education) j(experiment)
save "Temp\PV Earnings Variation SD.dta", replace
restore


*** BY PRODUCTIVITY
gen COV=.
gen GINI=.
tostring COV GINI , replace
foreach ed of numlist 1 2 3 {
foreach prod of numlist 1 2 3  {
cap inequal7 earnings_PV if experiment==0 &  education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==0 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==0 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==0 & education==`ed' & productivity_type==`prod'
}
}
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education productivity_type) 
sort education experiment productivity_type
reshape wide MEAN COV  GINI , i(education productivity_type) j(experiment)
append using "Temp\PV Earnings Variation SD.dta"
rename MEAN0 MEAN6 
rename COV0 COV6
rename GINI0 GINI6
replace productivity_type=0 if productivity_type==.
sort productivity_type education
gen order= 1 if 	education==0 & productivity_type==0
replace order= 2 if 	education==1 & productivity_type==0
replace order= 3 if 	education==2 & productivity_type==0
replace order= 4 if 	education==3 & productivity_type==0
replace order= 5 if 	education==1 & productivity_type==1
replace order= 6 if 	education==1 & productivity_type==2
replace order= 7 if 	education==1 & productivity_type==3
replace order= 8 if 	education==2 & productivity_type==1
replace order= 9 if 	education==2 & productivity_type==2
replace order= 10 if 	education==2 & productivity_type==3
replace order= 11 if 	education==3 & productivity_type==1
replace order= 12 if 	education==3 & productivity_type==2
replace order= 13 if 	education==3 & productivity_type==3
drop education productivity_type
sort order
save "Temp\PV Earnings Variation SD.dta", replace

}

* 2. prepare data from all relevant experiments
qui{

* table with pv earnings inequality, benchmark, no shocks - decision rules fixed, no shocks - decision rules adjust.
clear 
use "Experiments Stata Files/Experiment0.dta"
append using "Experiments Stata Files/Experiment05.dta"
append using "Experiments Stata Files/Experiment022.dta"
append using "Experiments Stata Files/Experiment024.dta"
append using "Experiments Stata Files/Experiment028.dta"
append using "Counterfactuals Stata Files/Counterfactual4.dta"


replace experiment=1 if experiment==22 // eliminate shocks, decision rules fixed, same LS and savings as benchmark
replace experiment=2 if experiment==28 // eliminate shocks, decision rules fixed, same HC as benchmark
replace experiment=3 if experiment==24 // eliminate shocks, decision rules fixed, same wage offers as benchmark
replace experiment=6 if experiment==4 // eliminate shocks, lucky, decision rules change

replace experiment=4 if experiment==5 // eliminate shocks, decision rules fixed
replace experiment=5 if experiment==6 // eliminate shocks, lucky, decision rules change

keep if age<=64

// eliminate everyone in experiment 22 who didn't live in benchmark
sort ID age experiment
drop if experiment==1 & age!=age[_n-1]

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
keep if age==25

preserve
inequal7 earnings_PV if experiment==0
gen COV=r(cov) if experiment==0
gen GINI=r(gini) if experiment==0
inequal7 earnings_PV if experiment==1
replace COV=r(cov) if experiment==1
replace GINI=r(gini) if experiment==1
inequal7 earnings_PV if experiment==2
replace COV=r(cov) if experiment==2
replace GINI=r(gini) if experiment==2
inequal7 earnings_PV if experiment==3
replace COV=r(cov) if experiment==3
replace GINI=r(gini) if experiment==3
inequal7 earnings_PV if experiment==4
replace COV=r(cov) if experiment==4
replace GINI=r(gini) if experiment==4
inequal7 earnings_PV if experiment==5
replace COV=r(cov) if experiment==5
replace GINI=r(gini) if experiment==5
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI , by(experiment) 
gen education=0
sort education experiment
save "Temp\PV Earnings Variation.dta", replace
restore 

* by education

preserve
gen COV=.
gen GINI=.
tostring COV  GINI, replace
foreach ed of numlist 1 2 3 {
inequal7 earnings_PV if experiment==0 &  education==`ed'
replace COV=r(cov) if experiment==0 & education==`ed'
replace GINI=r(gini) if experiment==0 & education==`ed'
inequal7 earnings_PV if experiment==1 & education==`ed'
replace COV=r(cov) if experiment==1 & education==`ed'
replace GINI=r(gini) if experiment==1 & education==`ed'
inequal7 earnings_PV if experiment==2 & education==`ed'
replace COV=r(cov) if experiment==2 & education==`ed'
replace GINI=r(gini) if experiment==2 & education==`ed'

inequal7 earnings_PV if experiment==3 & education==`ed'
replace COV=r(cov) if experiment==3 & education==`ed'
replace GINI=r(gini) if experiment==3 & education==`ed'

inequal7 earnings_PV if experiment==4 & education==`ed'
replace COV=r(cov) if experiment==4 & education==`ed'
replace GINI=r(gini) if experiment==4 & education==`ed'

inequal7 earnings_PV if experiment==5 & education==`ed'
replace COV=r(cov) if experiment==5 & education==`ed'
replace GINI=r(gini) if experiment==5 & education==`ed'

}
destring COV GINI , replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education) 
sort education experiment
append using "Temp\PV Earnings Variation.dta"
reshape wide MEAN COV  GINI , i(education) j(experiment)
save "Temp\PV Earnings Variation.dta", replace
restore


*** BY PRODUCTIVITY
gen COV=.
gen GINI=.
tostring COV GINI , replace
foreach ed of numlist 1 2 3 {
foreach prod of numlist 1 2 3  {
cap inequal7 earnings_PV if experiment==0 &  education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==0 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==0 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==0 & education==`ed' & productivity_type==`prod'
cap inequal7 earnings_PV if experiment==1 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==1 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==1 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==1 & education==`ed' & productivity_type==`prod'
cap inequal7 earnings_PV if experiment==2 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==2 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==2 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==2 & education==`ed' & productivity_type==`prod'

cap inequal7 earnings_PV if experiment==3 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==3 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==3 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==3 & education==`ed' & productivity_type==`prod'

cap inequal7 earnings_PV if experiment==4 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==4 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==4 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==4 & education==`ed' & productivity_type==`prod'

cap inequal7 earnings_PV if experiment==5 & education==`ed' & productivity_type==`prod'
cap replace COV=r(cov) if experiment==5 & education==`ed' & productivity_type==`prod'
cap replace MLD=r(mld) if experiment==5 & education==`ed' & productivity_type==`prod'
cap replace GINI=r(gini) if experiment==5 & education==`ed' & productivity_type==`prod'
}
}
destring COV  GINI, replace
collapse (mean) MEAN=earnings_PV COV  GINI, by(experiment education productivity_type) 
sort education experiment productivity_type
reshape wide MEAN COV  GINI , i(education productivity_type) j(experiment)
append using "Temp\PV Earnings Variation.dta"

* organize the data in the order we need for the table
replace productivity_type=0 if productivity_type==.
sort productivity_type education
gen order= 1 if 	education==0 & productivity_type==0
replace order= 2 if 	education==1 & productivity_type==0
replace order= 3 if 	education==2 & productivity_type==0
replace order= 4 if 	education==3 & productivity_type==0
replace order= 5 if 	education==1 & productivity_type==1
replace order= 6 if 	education==1 & productivity_type==2
replace order= 7 if 	education==1 & productivity_type==3
replace order= 8 if 	education==2 & productivity_type==1
replace order= 9 if 	education==2 & productivity_type==2
replace order= 10 if 	education==2 & productivity_type==3
replace order= 11 if 	education==3 & productivity_type==1
replace order= 12 if 	education==3 & productivity_type==2
replace order= 13 if 	education==3 & productivity_type==3
sort order
merge order using "Temp\PV Earnings Variation SD.dta" // merge with file we saved in step 1. 
drop _merge

save "Temp\PV Earnings Variation.dta", replace
}

* 3. Construct Table 4
qui{
clear 
use "Temp\PV Earnings Variation.dta"

* need to infer some effects from differences across experiments 
gen MEAN7=MEAN0 - ( (MEAN0- MEAN4 )- (MEAN0 - ( MEAN0-(MEAN6 - MEAN1))) - (MEAN0-( MEAN0-(MEAN2 - MEAN4))) )
gen MEAN8=MEAN0 -(MEAN2 - MEAN4)
gen MEAN9= MEAN0-(MEAN6-MEAN1)

keep education productivity_type MEAN0 MEAN7 MEAN8 MEAN9 MEAN4 MEAN5
order education productivity_type MEAN0 MEAN7 MEAN8 MEAN9 MEAN4 MEAN5

replace MEAN7=(MEAN7-MEAN0)/MEAN0*100
replace MEAN8=(MEAN8-MEAN0)/MEAN0*100
replace MEAN9=(MEAN9-MEAN0)/MEAN0*100
replace MEAN4=(MEAN4-MEAN0)/MEAN0*100
replace MEAN5=(MEAN5-MEAN0)/MEAN0*100

replace MEAN4= MEAN5-MEAN4 // behavioral effect

rename MEAN0 col1
rename MEAN7 col2
rename MEAN8 col3 
rename MEAN9 col4
rename MEAN4 col5
rename MEAN5 col6

* OPEN DATA AND FIND TABLE 4 - copy paste. all columns and rown in order
}

* 4. Construct Table 5
qui{
clear 
use "Temp\PV Earnings Variation.dta"

* need to infer some effects from differences across experiments 
gen COV7=COV0 - ( (COV0- COV4 )- (COV0 - ( COV0-(COV6 - COV1))) - (COV0-( COV0-(COV2 - COV4))) )
gen COV8=COV0 -(COV2 - COV4)
gen COV9= COV0-(COV6-COV1)

keep education productivity_type COV0 COV7 COV8 COV9 COV4 COV5
order education productivity_type COV0 COV7 COV8 COV9 COV4 COV5

replace COV7=(COV7-COV0)/COV0*100
replace COV8=(COV8-COV0)/COV0*100
replace COV9=(COV9-COV0)/COV0*100
replace COV4=(COV4-COV0)/COV0*100
replace COV5=(COV5-COV0)/COV0*100

replace COV4= COV5-COV4

drop if productivity_type>0

rename COV0 col1
rename COV7 col2
rename COV8 col3 
rename COV9 col4
rename COV4 col5
rename COV5 col6

* open data - copy paste into Table 5 
* rows and colums are in order
}
}




* Table 6 
* Emp and SI
qui{
clear 
use "Experiments Stata Files/Experiment0.dta"
append using "Experiments Stata Files/Experiment05.dta"
append using "Experiments Stata Files/Experiment028.dta"
append using "Counterfactuals Stata Files/Counterfactual4.dta"

replace experiment=1 if experiment==28
replace experiment=2 if experiment==5
replace experiment=3 if experiment==4
keep if age<=64

preserve
collapse (mean) employed_yn TR, by(experiment) 
sort  experiment
save "Temp\LS and SI.dta", replace 
restore

preserve
collapse (mean) employed_yn TR, by(experiment education) 
sort  experiment education
append using  "Temp\LS and SI.dta"
replace education=0 if education==.
reshape wide employed_yn TR, i(education) j(experiment)
save "Temp\LS and SI.dta", replace 
restore


collapse (mean) employed_yn TR, by(experiment education productivity_type) 
sort  experiment education productivity_type
reshape wide employed_yn TR, i(education productivity_type) j(experiment)
save "Temp\LS and SI 1.dta", replace
clear 
use "Temp\LS and SI.dta"
append using  "Temp\LS and SI 1.dta"

order education productivity_type employed_yn0  employed_yn1 TR0 TR1
replace employed_yn0=employed_yn0 * 100
replace employed_yn1=employed_yn1 * 100
replace employed_yn2=employed_yn2 * 100
replace employed_yn3=employed_yn3 * 100
replace TR0=TR0*100
replace TR1=TR1*100
replace TR2=TR2*100
replace TR3=TR3*100

order education productivity_type employed_yn0 employed_yn1 employed_yn2 employed_yn3 TR0 TR1 TR2 TR3

* open data and copy paste into table. Columns and rows are in order. 
}

